package com.example.tbtx.tbtx.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.tbtx.tbtx.R;


public class CloudProgressView extends View{

    public float progress;  //0-->1
    private Paint paint;
    private Bitmap bg_right;
    private Bitmap bg_left;
    private ValueAnimator valueAnimator;
    private ValueAnimator valueAnimator2;
    private Handler handler;
    public static final String TAG="fs666";
    public CloudProgressView(Context context) {
        this(context,null);
    }

    public CloudProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public StartActivityCallBack callBack;

    public void setCallBack(StartActivityCallBack callBack) {
        this.callBack = callBack;
    }

    private void init(Context context) {
        handler = new Handler(context.getMainLooper()){
            @Override
            public void dispatchMessage(Message msg) {
                super.dispatchMessage(msg);
                valueAnimator2.start();

            }
        };
        bg_right = BitmapFactory.decodeResource(context.getResources(), R.drawable.backgroundleft);
        bg_left = BitmapFactory.decodeResource(context.getResources(), R.drawable.backgroundright);
        paint = new Paint();
        setLayerType(View.LAYER_TYPE_SOFTWARE,paint);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(bg_left,null,new Rect(0,0, (int) (getMeasuredWidth()*progress),getMeasuredHeight()),paint);
        canvas.drawBitmap(bg_right,null,new Rect((int) (getMeasuredWidth()*(1-progress)),0, (getMeasuredWidth()),getMeasuredHeight()),paint);
    }

    public void startAnima(final AnimatorCallBack animatorCallBack){

        valueAnimator = ValueAnimator.ofFloat(0, 1).setDuration(1500);
        valueAnimator2 = ValueAnimator.ofFloat(1, 0).setDuration(1500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
               progress= (float) animation.getAnimatedValue();
               postInvalidate();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
//                systemService.removeView(CloudProgressView.this);
                callBack.startActivity();
                handler.sendEmptyMessageDelayed(0,600);
            }
        });
        valueAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                progress= (float) animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: ");
                postInvalidate();
            }
        });
        valueAnimator2.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                animatorCallBack.animatorEnd();
            }
        });
        valueAnimator.start();
    }

    public interface StartActivityCallBack{
        public void startActivity();
    }

    public interface AnimatorCallBack{
        void animatorEnd();
    }

}
